<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript 类(class)-类的方法</title>
</head>

<body>
    <h2>JavaScript 类的方法</h2>

    <p>类中如何定义方法</p>

    <p id="demo"></p>

    <script>
        class Runoob {
            constructor(name, year) {
                this.name = name;
                this.year = year;
            }
            age() {
                let date = new Date();
                return date.getFullYear() - this.year;
            }
        }

        let runoob = new Runoob("菜鸟教程", 2018);
        document.getElementById("demo").innerHTML =
            "菜鸟教程 " + runoob.age() + " 岁了。";
    </script>
</body>

</html>